<template>
  <view class="page">
    <image mode="widthFix" style="width: 100vw" :show-menu-by-longpress="true" :src="getShowImage(posterUrl)"></image>
    <view class="btn" @click="receive">立即领取</view>
  </view>
</template>

<script>
import request from "@/utils/request"
import { getShowImage, showModal } from "@/utils"
import store from "@/store"
import { dictActionType } from "@/store/modules/dict"
import shareInfo from "@/utils/shareInfo"
import { getShowNginxPicUrl } from "@/utils/settings"

export default {
  data() {
    return {
      queryParams: {
        couponId: "",
      },
      redirectUrl: "",
      posterUrl: "",
    }
  },
  methods: {
    getShowImage,
    receive() {
      //发送请求领取优惠券
      request({
        url: "userCoupon/receiveCouponByCouponId",
        data: this.queryParams,
      })
        .then(async () => {
          await showModal({
            content: "领取成功",
            showCancel: false,
            confirmText: "确定",
          })
          uni.redirectTo({
            url: this.redirectUrl,
          })
          uni.switchTab({
            url: this.redirectUrl,
          })
        })
        .catch(async (err) => {
          await showModal({
            content: err,
            showCancel: false,
            confirmText: "确定",
          })
          uni.redirectTo({
            url: this.redirectUrl,
          })
          uni.switchTab({
            url: this.redirectUrl,
          })
        })
    },
  },
  async onLoad(query) {
    this.queryParams.couponId = query.couponId
    if (query.redirectUrl) {
      this.redirectUrl = decodeURIComponent(query.redirectUrl)
    } else {
      this.redirectUrl = "/pages/index/index"
    }
		if (query.posterUrl) {
			this.posterUrl = getShowNginxPicUrl(decodeURIComponent(query.posterUrl))
		} else {
			this.posterUrl = getShowNginxPicUrl('a6922db0efc1e7166391e26c9cee8bf.png') 
		}
    //没有配置领取优惠券海报则直接领取
    if (!this.posterUrl) {
      this.receive()
    }
  },
	async onShareAppMessage() {
		return await shareInfo.getDefaultShareInfo()
	}
}
</script>

<style lang="scss" scoped>
.page {
	padding-bottom: calc(env(safe-area-inset-bottom) + 150rpx);
}
.btn {
	position: fixed;
	bottom: calc(env(safe-area-inset-bottom) + 30rpx);
	left: 50%;
	transform: translateX(-50%);
	padding: 0 80rpx;
	height: 100rpx;
	line-height: 100rpx;
	text-align: center;
	background-color: #ff4d4f;
	color: #fff;
	border-radius: 50rpx;
}
</style>
